package com.xyht.sca_s.student_manage_system.modules.welcomeNewStudent.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xyht.sca_s.student_manage_system.common.commonReturn.CommonCodeEnum;
import com.xyht.sca_s.student_manage_system.common.commonReturn.CommonResult;
import com.xyht.sca_s.student_manage_system.common.commonReturn.ExceptionCast;
import com.xyht.sca_s.student_manage_system.common.commonReturn.ResponseResult;
import com.xyht.sca_s.student_manage_system.modules.welcomeNewStudent.entity.SmsWnsRegistrationProcess;
import com.xyht.sca_s.student_manage_system.modules.welcomeNewStudent.entity.req.SmsWnsRegistrationProcessReq;
import com.xyht.sca_s.student_manage_system.modules.welcomeNewStudent.entity.resp.SmsWnsRegistrationProcessResp;
import com.xyht.sca_s.student_manage_system.modules.welcomeNewStudent.mapper.SmsWnsRegistrationProcessMapper;
import com.xyht.sca_s.student_manage_system.modules.welcomeNewStudent.service.SmsWnsRegistrationProcessService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

import static com.xyht.sca_s.student_manage_system.common.util.ParamCheckUtil.isNullOrEmpty;

/**
 * <p>
 * 报到流程步骤表 服务实现类
 * </p>
 *
 * @author XYHT
 * @since 2024-01-08
 */
@Service
public class SmsWnsRegistrationProcessServiceImpl extends ServiceImpl<SmsWnsRegistrationProcessMapper, SmsWnsRegistrationProcess> implements SmsWnsRegistrationProcessService {

    @Override
    @Transactional
    public ResponseResult addProcess(SmsWnsRegistrationProcessReq processReq) {
        if (isNullOrEmpty(processReq.getStepName(), processReq.getSort(), processReq.getIsMandatory(), processReq.getIsUse())) {
            return CommonResult.failed(CommonCodeEnum.INVALID_PARAM);
        }

        // 校验名称是否存在
        List<SmsWnsRegistrationProcess> processList = this.list(new LambdaQueryWrapper<SmsWnsRegistrationProcess>()
                .eq(SmsWnsRegistrationProcess::getStepName, processReq.getStepName()));
        if (!processList.isEmpty()) {
            return CommonResult.failed(CommonCodeEnum.WNS_PROCESS_NAME_NOT_EXIST);
        }

        // 校验顺序是否冲突
        List<SmsWnsRegistrationProcess> sortProcessList = this.list(new LambdaQueryWrapper<SmsWnsRegistrationProcess>()
                .ge(SmsWnsRegistrationProcess::getSort, processReq.getSort()));
        if (!sortProcessList.isEmpty()) {
            // 大于当前顺序的每个流程 顺序+1
            for (SmsWnsRegistrationProcess smsWnsRegistrationProcess : sortProcessList) {
                smsWnsRegistrationProcess.setSort(smsWnsRegistrationProcess.getSort() + 1);
            }
            boolean sortUpdSuccess = this.updateBatchById(sortProcessList);
            if (!sortUpdSuccess) {
                ExceptionCast.cast(CommonResult.failed(CommonCodeEnum.FAIL));
            }
        }

        // 保存当前流程
        SmsWnsRegistrationProcess process = new SmsWnsRegistrationProcess();
        BeanUtils.copyProperties(processReq, process);
        if (processReq.getSort() == null) {
            int count = this.count();
            processReq.setSort(count + 1);
        }
        boolean processSaveSuccess = this.save(process);
        if (!processSaveSuccess) {
            ExceptionCast.cast(CommonResult.failed(CommonCodeEnum.FAIL));
        }

        return CommonResult.success(process.getId());
    }

    @Override
    @Transactional
    public ResponseResult updateProcess(SmsWnsRegistrationProcessReq processReq) {
        if (isNullOrEmpty(processReq.getId(), processReq.getStepName(), processReq.getSort(), processReq.getIsMandatory(), processReq.getIsUse())) {
            return CommonResult.failed(CommonCodeEnum.INVALID_PARAM);
        }

        // 校验流程是否存在
        SmsWnsRegistrationProcess process = this.getById(processReq.getId());
        if (process == null) {
            return CommonResult.failed(CommonCodeEnum.WNS_PROCESS_NOT_EXIST);
        }

        // 校验名称是否存在
        List<SmsWnsRegistrationProcess> processList = this.list(new LambdaQueryWrapper<SmsWnsRegistrationProcess>()
                .eq(SmsWnsRegistrationProcess::getStepName, processReq.getStepName())
                .ne(SmsWnsRegistrationProcess::getId, processReq.getId()));
        if (!processList.isEmpty()) {
            return CommonResult.failed(CommonCodeEnum.WNS_PROCESS_NAME_NOT_EXIST);
        }

        // 校验顺序是否冲突
        List<SmsWnsRegistrationProcess> conflictProcessList = this.list(new LambdaQueryWrapper<SmsWnsRegistrationProcess>()
                .eq(SmsWnsRegistrationProcess::getSort, processReq.getSort()));
        if (!conflictProcessList.isEmpty()) {
            // 顺序进行替换
            SmsWnsRegistrationProcess conflictProcess = conflictProcessList.get(0);
            conflictProcess.setSort(process.getSort());
            boolean sortUpdSuccess = this.updateBatchById(conflictProcessList);
            if (!sortUpdSuccess) {
                ExceptionCast.cast(CommonResult.failed(CommonCodeEnum.FAIL));
            }
        }

        // 修改当前流程
        BeanUtils.copyProperties(processReq, process, "id");
        boolean processUpdSuccess = this.updateById(process);
        if (!processUpdSuccess) {
            ExceptionCast.cast(CommonResult.failed(CommonCodeEnum.FAIL));
        }

        return CommonResult.success(process.getId());
    }

    @Override
    public ResponseResult listProcess(Integer pageNum, Integer pageSize, String searchStr) {
        LambdaQueryWrapper<SmsWnsRegistrationProcess> queryWrapper = new LambdaQueryWrapper<SmsWnsRegistrationProcess>()
                .orderByAsc(SmsWnsRegistrationProcess::getSort);

        if (!isNullOrEmpty(searchStr)) {
            StringUtils.deleteWhitespace(searchStr);
            queryWrapper.and(i -> i.like(SmsWnsRegistrationProcess::getStepName, searchStr).or()
                    .like(SmsWnsRegistrationProcess::getDescription, searchStr));
        }

        Page<SmsWnsRegistrationProcess> page = new Page<>(pageNum, pageSize);
        this.page(page, queryWrapper);
        List<SmsWnsRegistrationProcessResp> processRespList = page.getRecords().stream().map(process -> {
            SmsWnsRegistrationProcessResp processResp = new SmsWnsRegistrationProcessResp();
            BeanUtils.copyProperties(process, processResp);

            return processResp;
        }).collect(Collectors.toList());

        return CommonResult.success(processRespList, (int) page.getTotal());
    }

    @Override
    public ResponseResult processDetail(String processId) {
        SmsWnsRegistrationProcess process = this.getById(processId);
        if (process == null) {
            return CommonResult.failed(CommonCodeEnum.WNS_PROCESS_NOT_EXIST);
        }

        SmsWnsRegistrationProcessResp processResp = new SmsWnsRegistrationProcessResp();
        BeanUtils.copyProperties(process, processResp);

        return CommonResult.success(processResp);
    }
}
